<!-- 按交期分析 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" style="width: 100%">
        <Table
          :outTableHeight="10"
          :summaryData="summaryData"
          summary
          :tableHeader="tableHeader"
          :tableData="tableData"
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
        />
      </el-card>
    </div>
    <el-drawer v-model="drawer" :direction="'ttb'" size="small" :with-header="false">
      <span @click="drawer = false" style="float: right; cursor: pointer; color: rgb(7, 7, 7)">X</span>
      <div class="flex AIC" style="flex-direction: column; width: 100%">
        <div>
          <el-button class="ml10" type="primary" size="default" @click="exportExcel">导出</el-button>
        </div>
        <div class="flex mt10 JCC" style="width: 100%">
          <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" style="width: 100%">
            <JTable @currentRow="currentRow2" :isLoading="isLoading2" :tableHeader="tableHeader2" :tableData="tableData2" class="mt10" />
          </el-card>
        </div>
      </div>
    </el-drawer>

    <el-drawer v-model="drawer2" :direction="'ttb'" size="small" :with-header="false">
      <div class="flex AIC" style="flex-direction: column; width: 100%">
        <div>
          <el-button class="ml10" type="primary" size="default" @click="exportExcel2">导出</el-button>
        </div>
        <div class="flex mt10 JCC" style="width: 100%">
          <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" style="width: 100%">
            <JTable @currentRow="currentRow3" :isLoading="isLoading3" :tableHeader="tableHeader3" :tableData="tableData3" class="mt10" />
          </el-card>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script lang="ts">
import { onMounted, ref } from 'vue'
import { watch } from 'vue'
import { debounce } from 'lodash'
import service from '@/utils/request'
import fileRequest from '@/utils/fileRequest'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
import * as echarts from 'echarts'

export default {
  name: 'AnalysisDeliveryDate'
}
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
const tableHeader = ref([
  {
    label: '交期月份',
    prop: 'time'
  },
  {
    label: '接单数',
    prop: 'tQty'
  },
  {
    label: '已完成',
    prop: 'comQty'
  },
  {
    label: '未完成',
    prop: 'unComQty'
  },
  {
    Multistage: true,
    label: '各流程节点未完成',
    children: [
      {
        label: '版房',
        prop: 'unBfQty'
      },
      {
        label: '备料',
        prop: 'unBlQty'
      },
      {
        label: '裁断',
        prop: 'unCdQty'
      },
      {
        label: '配套',
        prop: 'unPtQty'
      },
      {
        label: '针车',
        prop: 'unZcQty'
      },
      {
        label: '成型',
        prop: 'unCxQty'
      }
    ]
  }
])
const getCurrentYearMonth = () => {
  const date = new Date()
  const year = date.getFullYear().toString() // 获取年份的后两位
  return `${year}`
}
const formList = ref([
  {
    dateType: 'year',
    type: 'dateMonth',
    dateLabel: '年份',
    dateValue: getCurrentYearMonth()
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])
// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/statements/downloadStitchMonthProd?month=${formList.value[0].dateValue}&prodSeq=${3}`)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 1,
    num: 0
  },
  {
    index: 2,
    num: 0
  },
  {
    index: 3,
    num: 0
  },
  {
    index: 4,
    num: 0
  },
  {
    index: 5,
    num: 0
  },
  {
    index: 6,
    num: 0
  },
  {
    index: 7,
    num: 0
  },
  {
    index: 8,
    num: 0
  },
  {
    index: 9,
    num: 0
  }
])
const summaryData2 = ref<summaryDataType[]>([
  {
    index: 6,
    num: 0
  },
  {
    index: 7,
    num: 0
  },
  {
    index: 8,
    num: 0
  },
  {
    index: 9,
    num: 0
  },
  {
    index: 10,
    num: 0
  },
  {
    index: 11,
    num: 0
  },
  {
    index: 12,
    num: 0
  },
  {
    index: 13,
    num: 0
  },
  {
    index: 14,
    num: 0
  },
  {
    index: 15,
    num: 0
  },
  {
    index: 16,
    num: 0
  },
  {
    index: 17,
    num: 0
  },
  {
    index: 18,
    num: 0
  },
  {
    index: 19,
    num: 0
  },
  {
    index: 20,
    num: 0
  },
  {
    index: 21,
    num: 0
  },
  {
    index: 22,
    num: 0
  },
  {
    index: 23,
    num: 0
  },
  {
    index: 24,
    num: 0
  },
  {
    index: 25,
    num: 0
  },
  {
    index: 26,
    num: 0
  },
  {
    index: 27,
    num: 0
  },
  {
    index: 28,
    num: 0
  },
  {
    index: 29,
    num: 0
  },
  {
    index: 30,
    num: 0
  },
  {
    index: 31,
    num: 0
  },
  {
    index: 32,
    num: 0
  },
  {
    index: 33,
    num: 0
  },
  {
    index: 34,
    num: 0
  },
  {
    index: 35,
    num: 0
  },
  {
    index: 36,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}

// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>(
    `/statements/prodDayProdByYear?year=${
      formList.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '年份')?.dateValue
    }`
  )
  isLoading.value = false
  tableData.value = tableDataList.list.map((item: any) => {
    item.blue = true
    return item
  })
  summaryData.value[0].num = tableDataList.tTotal
  summaryData.value[1].num = tableDataList.cTotal
  summaryData.value[2].num = tableDataList.ucTotal
  summaryData.value[3].num = tableDataList.bfTotal
  summaryData.value[4].num = tableDataList.blTotal
  summaryData.value[5].num = tableDataList.cdTotal
  summaryData.value[6].num = tableDataList.ptTotal
  summaryData.value[7].num = tableDataList.zcTotal
  summaryData.value[8].num = tableDataList.cxTotal
  // total.value = tableDataList.total
  // currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const choose = ref(0)
const currentProp = ref('')
const currentRow = async (row: any = tableData.value[0], scop: any = {}, item: any) => {
  currentTableRow.value = row
  drawer.value = true
  getTableList2(row, scop, item)
}
const drawer = ref(false)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData9 = ref<summaryDataType[]>([
  {
    index: 8,
    num: 0
  }
])
const cutDown = ref('')
const isLoading2 = ref(false)
const tableData2 = ref([])
const tableHeader2 = ref([
  {
    label: '品牌',
    prop: 'brand'
  },
  {
    label: '接单数',
    prop: 'tQty'
  },
  {
    label: '已完成',
    prop: 'comQty'
  },
  {
    label: '未完成',
    prop: 'unComQty'
  },
  {
    Multistage: true,
    label: '各流程节点未完成',
    children: [
      {
        label: '版房',
        prop: 'unBfQty'
      },
      {
        label: '备料',
        prop: 'unBlQty'
      },
      {
        label: '裁断',
        prop: 'unCdQty'
      },
      {
        label: '配套',
        prop: 'unPtQty'
      },
      {
        label: '针车',
        prop: 'unZcQty'
      },
      {
        label: '成型',
        prop: 'unCxQty'
      }
    ]
  }
])
const currentMonth = ref('')
const getTableList2 = debounce(async (row: any, columns: any, item: any) => {
  isLoading2.value = true
  currentMonth.value = row.time
  // 将row.month的YYYYMM转换为YYYY-MM
  let res = await LoginWork.post<any, any>(`/statements/prodDayProdByBrand?month=${row.time}`)
  if (res.code === 200) {
    tableData2.value = res.data.map((item: any) => {
      item.blue = true
      return item
    })
    isLoading2.value = false
  } else {
    JMessage.error(res.message)
    isLoading2.value = false
    return
  }
}, 500)
const exportExcel = async () => {
  let month = formList.value[0].dateValue
  const res = (await fileRequest.post(
    `/statements/downloadStitchMonthProdTotal?choose=${choose.value}&month=${month}&prodSeq=${3}&workerId=${
      currentTableRow.value.workerId
    }&workerName=${currentTableRow.value.workerName}`
  )) as any
  const link = document.createElement('a') // 利用a标签自定义下载文件名
  let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
  link.style.display = 'none'
  //设置连接
  link.href = window.URL.createObjectURL(blob)
  const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
  link.download = FileName //自定义文件名字
  document.body.appendChild(link)
  //模拟点击事件
  link.click()
  document.body.removeChild(link)
}
const currentTableRow2 = ref({}) as any
const currentRow2 = async (row: any = tableData2.value[0], scop: any = {}, item: any) => {
  currentTableRow2.value = row
  drawer2.value = true
  getTableList3(row, scop, item)
}

const drawer2 = ref(false)
const tableHeader3 = ref([
  {
    width: 160,
    label: '指令号',
    prop: 'orderId'
  },
  {
    label: '鞋图',
    prop: 'shoeName'
  },
  {
    label: '季度',
    prop: 'seasonType'
  },
  {
    label: '品牌',
    prop: 'brand'
  },
  {
    label: '样品类型',
    prop: 'sampleType'
  },
  {
    label: '工厂货号',
    prop: 'style'
  },
  {
    label: '颜色',
    prop: 'color'
  },
  {
    label: '号码',
    prop: 'sizeNo'
  },
  {
    label: '订单数',
    prop: 'orderQty'
  },
  {
    label: '未完成',
    prop: 'unQty'
  },
  {
    label: '计划日期',
    prop: 'planDate'
  }
])
const tableData3 = ref([])
const isLoading3 = ref(false)
const getTableList3 = debounce(async (row: any, columns: any, item: any) => {
  let prodSeq = 0
  switch (columns.label) {
    case '版房':
      prodSeq = 1
      break
    case '备料':
      prodSeq = 2
      break
    case '裁断':
      prodSeq = 3
      break
    case '配套':
      prodSeq = 8
      break
    case '针车':
      prodSeq = 10
      break
    case '成型':
      prodSeq = 12
      break
    default:
      prodSeq = 0
  }
  isLoading3.value = true
  let res = await LoginWork.post<any, any>(`/statements/prodDayProdByBrandDetail?month=${currentMonth.value}&prodSeq=${prodSeq}`)
  tableData3.value = res.data.map((item: any) => {
    item.blue = true
    item.orderQty = Number(item.orderQty).toFixed(2)
    item.unQty = Number(item.unQty).toFixed(2)
    return item
  })
  isLoading3.value = false
}, 500)
</script>
